import {Component, OnInit} from '@angular/core';
import {OperateService} from '../operate.service';
import {DatePipe} from '@angular/common';
import {NzMessageService} from 'ng-zorro-antd/message';

@Component({
  selector: 'app-activity-config',
  templateUrl: './activity-config.component.html',
  styleUrls: ['./activity-config.component.scss']
})
export class ActivityConfigComponent implements OnInit {
  tableData = [];
  editModal = {
    visible: false,
    id: '',
    title: '',
    activityType: 1,
    integralMultiple: '',
    time: [] as any[]
  };
  STATUS = ['未开始', '进行中', '已删除', '已结束'];

  constructor(
    private operateService: OperateService,
    private datePipe: DatePipe,
    private nzMessageService: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.getTable();
  }

  getTable() {
    this.operateService.getActivityConfig().then((res: any) => {
      if (res.code === 0) {
        this.tableData = res.data;
      }
    });
  }

  showActivityModal(data?: any) {
    if (data) {
      this.editModal = {
        visible: true,
        id: data.id,
        title: data.title,
        activityType: data.activityType,
        integralMultiple: data.integralMultiple,
        time: [data.beginTime, data.endTime]
      };
    }
    this.editModal.visible = true;
  }

  hideActivityModal() {
    this.editModal = {
      visible: false,
      id: '',
      title: '',
      activityType: 1,
      integralMultiple: '',
      time: []
    };
  }

  saveActivity() {
    if (!this.editModal.title) {
      this.nzMessageService.error('请输入活动名称');
    }
    if (!this.editModal.integralMultiple) {
      this.nzMessageService.error('请输入积分设置');
    }
    if (!this.editModal.time.length) {
      this.nzMessageService.error('请选择生效时间');
    }
    this.operateService.editActivityConfig({
      id: this.editModal.id,
      title: this.editModal.title,
      activityType: this.editModal.activityType,
      integralMultiple: this.editModal.integralMultiple,
      beginTime: this.datePipe.transform(this.editModal.time[0], 'yyyy-MM-dd HH:mm:ss'),
      endTime: this.datePipe.transform(this.editModal.time[1], 'yyyy-MM-dd HH:mm:ss'),
    }).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.hideActivityModal();
        this.getTable();
      }
    });
  }
}
